<?php
class Zfarm_ModelController extends Zend_Controller_Action
{
        private function normalizeTableName($tablename){
                $tablename = str_replace(array('-', '_'), ' ', $tablename);
                $tablename = ucwords($tablename);
                $tablename = str_replace(' ', '', $tablename);
                return ucfirst($tablename);
        }
        private function getModulesList(){
            $modules = array();
            foreach ($this->_helper->AssetsList->getList() as $key => $value) {
                if($key != 'zfarm'){
                    $modules[$key] = $key;
                }
            }
            return $modules;
        }
        public function indexAction(){
            $this->_helper->viewRenderer->setNoRender();
            $db = Zend_Db_Table::getDefaultAdapter();
            $dbName = $db->fetchOne("select DATABASE();");
            $tables = $db->query('SHOW TABLES');
            $form = new Zend_Form;
            $form->setAction('');
            $form->setMethod('post');
            foreach($tables as $tables){
                $form->addElement('text', $this->normalizeTableName($tables["Tables_in_$dbName"])."_value", array('label' => $tables["Tables_in_$dbName"], 'value' => $this->normalizeTableName($tables["Tables_in_$dbName"])));
                $form->addElement(new Zend_Form_Element_Checkbox($this->normalizeTableName($tables["Tables_in_$dbName"]).'_check'));
                $modules = new Zend_Form_Element_Select($this->normalizeTableName($tables["Tables_in_$dbName"]).'_module');
                $modules->addMultiOptions($this->getModulesList());
                $form->addElement($modules); 
            }
            //$this->view->form = $form;
            $form->addElement('submit', 'Create');
            echo $form;
            
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($_POST)) {
                    $values = $form->getValues();
                    foreach($values as $key => $values){
                        $item = explode("_", $key);
                        $itens[$item[0]][$item[1]] = $values;
                    }
                    foreach($itens as $itens){
                        if($itens['check'] == '1'){
                            $this->defaultLayout($itens['value'], $itens['module']);
                        }
                    }
                }
            }
        }
       
        public function defaultLayout($table, $module = 'default'){
            if($module == 'default'){
                $moduleName = '';
                $moduleDir  = APPLICATION_PATH.'/models';
            }else{
                $moduleName = $module.'_';
                $moduleDir  = APPLICATION_PATH.'/'.$module.'/models';
            }
            $normalizedTableName = $this->normalizeTableName($table);
            $foo      = new Zend_CodeGenerator_Php_Class();
            $docblock = new Zend_CodeGenerator_Php_Docblock(array(
                'shortDescription' => $normalizedTableName.' model class',
                'longDescription'  => 'This is a class generated with ZFarm.',
                'tags'             => array(
                    array(
                        'name'        => 'version',
                        'description' => '$Rev:$',
                    ),
                    array(
                        'name'        => 'license',
                        'description' => 'MIT',
                    ),
                ),
            ));
            
            $foo->setName($moduleName.'Model_DbTable_'.$normalizedTableName);
            $foo->setExtendedClass('Zend_Db_Table_Abstract');
            $foo->setDocblock($docblock);
            $foo->setProperties(array(
                array(
                    'name'         => '_name',
                    'visibility'   => 'protected',
                    'defaultValue' => $table,
                ),
        ));
        $foo->generate();
        $file = new Zend_CodeGenerator_Php_File(array('classes'  => array($foo)));
        file_put_contents($moduleDir.'/DbTable/'.$normalizedTableName.'.php', $file);
    }
}
